const Service = require('egg').Service;

class UtilityService extends Service {
    /********************   常用   ********************/
    //生成http请求的响应体
    responseBody(code, msg, data = null, tips = null) {  //code:0失败1成功，其他自定义；msg:提示信息（中文）；data：返回的数据；err：错误信息（英文）
        return { code, msg, data, tips }
    }
    //转换字符串书写模式
    //toModeType: modeType、ModeType、mode_type
    toWriteMode(str, toModeType = "ModeType") {
        let strArr = [], endStr = "";
        while (str.indexOf("_") !== -1) {   //先把用_分隔开的字符串统一转为用大写字母隔开的字符串
            let ind = str.indexOf("_");
            str = str.replace("_", "");
            str = str.substring(0, ind) + str.substr(ind, 1).toUpperCase() + str.substr(ind + 1);
        }
        // 对用大写字母隔开的字符串分隔开，并装在strArr数组中
        let indsArr = [];
        for (let i = 0; i < str.length; i++) {
            let charCode = str.charCodeAt(i)
            if (charCode >= 65 && charCode <= 90) {   //这个区间内为大写字母
                i != 0 && indsArr.push(i)
            }
        }
        let sInd = 0, eInd = 0;
        let length = indsArr.length + 1
        for (let i = 0; i < length; i++) {
            eInd = i == length - 1 ? str.length : indsArr[i];
            strArr.push(str.substring(sInd, eInd));
            sInd = eInd;
        }
        switch (toModeType) {
            case "mode_type":
                strArr = strArr.map(item => {
                    return item.toLowerCase();
                });
                endStr = strArr.join("_");
                break;
            case "modeType":
                strArr = strArr.map((item, ind) => {
                    item.toLowerCase();
                    if (ind != 0) {
                        item = item.charAt(0).toUpperCase() + item.substr(1);
                    }
                    return item;
                })
                endStr = strArr.join("");
                break;
            case "ModeType":
                strArr = strArr.map(item => {
                    return item.charAt(0).toUpperCase() + item.substr(1);
                })
                endStr = strArr.join("");
                break;
            default: throw new Error(`暂不支持转换成${toModeType}类型`); break;
        }
        return endStr;
    }
    //获取最值（最大值、最小值）
    //type: all,min,max
    getValest(arr, type = "min") {
        let min = arr[0], max = arr[0],end=null;
        arr.forEach(item => {
            if (item < min) min = item;
            if (item > max) max = item;
        });
        if (type == "all") {
            end = { min, max };
        } else if (type == "min") {
            end = min;
        } else if (type == "max") {
            end = max;
        }
        return end;
    }
    //输出类型种类：undefined,boolean,number,nan,string,symbol,bigint,null,array,function,object
    getValType(val) {
        //typeof能判断出 undefined、boolean、number、string、symbol、bigint、function、object
        let type = typeof val;    //能判断出 undefined、boolean、number、string、symbol、bigint、function、object
        if (type === "object") {
            if (val === null) {
                type = "null";
            } else if (val instanceof Array) {
                type = "array";
            } else if (val instanceof Object) {
                type == "object";
            }
        } else if (type === "number") {
            if (isNaN(val)) {
                type = "nan";
            }
        }
        return type;
    }
}
module.exports = UtilityService;



